package com.wgo.bpot.domain.common;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import com.wgo.bpot.common.transport.exception.InvalidServicePrivilegeException;

public class ServicePrivilegeCollection {
    private static final transient Logger log = Logger.getLogger(ServicePrivilegeCollection.class.getName());
    private Set<ServicePrivilege> servicePrivileges = new HashSet<ServicePrivilege>();

    public boolean add(ServicePrivilege servicePrivilege){
        try {
//            if (servicePrivilege.isApplicable(null, null, null, null)) {
//                servicePrivilege.isAllowed(null, null, null, null);
//            }
            for (ServicePrivilege privilege : servicePrivileges) {
                if (privilege.getName().equals(servicePrivilege.getName())) {
                    return false;
                }
            }
            servicePrivileges.add(servicePrivilege);
        } catch (NullPointerException e) {
            throw new InvalidServicePrivilegeException("The service privilege is not valid. It does not accept null arguments!",e);
        }
        return true;
    }

    public void addAll(ServicePrivilegeCollection servicePrivilegeCollection) {
        addAll(servicePrivilegeCollection.getServicePrivileges());
    }

    public void addAll(Set<ServicePrivilege> servicePrivileges) {
        for (ServicePrivilege privilege : servicePrivileges) {
            add(privilege);
        }
    }

    public void remove(ServicePrivilege servicePrivilege){
        servicePrivileges.remove(servicePrivilege);
    }

    public Set<ServicePrivilege> getServicePrivileges(){
        return new HashSet<ServicePrivilege>(servicePrivileges);
    }

    public boolean isAllowed(ServicePrivilege...servicePrivileges){
        for (ServicePrivilege privilege : servicePrivileges) {
            boolean isOk = false;
            for (ServicePrivilege internalPrivilege : this.servicePrivileges) {
                if (privilege.getName().equals(internalPrivilege.getName())) {
                    isOk = true;
                }
            }
            if (!isOk) {
                return false;
            }
        }
        return true;
    }

//    /**
//     * This will return true unless the arguments are applicable AND not allowed
//     * @return
//     */
//    public <T> boolean isAllowedIfApplicable(Service service, CrudOperation operation, Class<T> conceptType, T conceptInstance){
//        boolean result = false;
//        int noAllowed = 0;
//        int noUnallowed = 0;
//        for (ServicePrivilege servicePrivilege : servicePrivileges) {
//            if (servicePrivilege.isApplicable(service, operation, conceptType, conceptInstance)){
//                if (servicePrivilege.isAllowed(service, operation, conceptType, conceptInstance)) {
//                    ++noAllowed;
//                } else {
//                    ++noUnallowed;
//                }
//            }
//        }
//        result = (0 < noAllowed);
//        if ((0 < noAllowed) && (0 < noUnallowed)) {
//            log.severe("There are " + noAllowed + " service privileges considering the call to service '" + service + "' allowed, and " + noUnallowed + " service privileges considering it not allowed. The end result is: allowed=" + result);
//        }
//        return result;
//    }

}
